package io.renren.modules.zhongyi.controller;

import io.renren.common.utils.R;
import io.renren.modules.zhongyi.entity.*;
import io.renren.modules.zhongyi.service.ZYLearnService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/zy_learn")
@Api(tags = "学习")
public class ZyLearnController {

    @Autowired
    ZYLearnService ZYLearnService;

    @GetMapping("courseList")
    @ApiOperation("获取课程列表")
    public R getCourseListData(){

        return R.ok().put("data", ZYLearnService.getLearnListData());
    }


    @PostMapping("course_audio_introduce")
    @ApiOperation("获取音频课程目录")
    public R getCourseAudioData(@RequestBody Map map){
        System.out.println("----------------" + map.get("id"));
        String id = (String) map.get("id");
        if (id == null || id.length()<=0){
            return R.ok("课程id不能为空");
        }

        AudioEntity audioEntity = ZYLearnService.getAudioDetailData(id);

        List<AudioChapterBean> beans = audioEntity.getBeans();
        if (beans==null){
            List<AudioChapterBean> chapterBeans = ZYLearnService.getAudioChapterData(id);
            audioEntity.setBeans(chapterBeans);
        }

        return R.ok().put("data",audioEntity);
    }

    @PostMapping("course_vedio_introduce")
    @ApiOperation("获取视频课程目录")
    public R getCourseVedioData(@RequestBody Map map){

        String id = (String) map.get("id");
        if (id == null || id.length()<=0){
            return R.ok("课程id不能为空");
        }

        VedioEntity vedioEntity = ZYLearnService.getVedioDetailData(id);

        List<VedioChapterBean> beans = vedioEntity.getChapterBeans();
        if (beans==null){
            List<VedioChapterBean> chapterBeans = ZYLearnService.getVedioChapterData(id);
            vedioEntity.setChapterBeans(chapterBeans);
        }

        return R.ok().put("data",vedioEntity);
    }

    @GetMapping("banner")
    @ApiOperation("获取学习页面轮播图")
    public R getBannerData(){

        return R.ok().put("data", ZYLearnService.getBannerData());
    }

    @PostMapping("courseCollection")
    @ApiOperation("课程收藏")
    public R setCourseCollectionData(@RequestBody Map map){
        String openId = (String) map.get("openId");
        String courseId = (String) map.get("courseId");
        String courseType = (String) map.get("courseType");
        String collectionType = (String) map.get("collectionType");
        String courseName = (String) map.get("courseName");
        if (openId==null || openId.length()<=0){
            return R.ok("openId 不存在");
        }
        if (courseId==null || courseId.length()<=0){
            return R.ok("courseId 不存在");
        }
        CollectionBean bean = new CollectionBean();
        bean.setOpenId(openId);
        bean.setCourseId(courseId);
        bean.setCourseType(courseType);
        bean.setCollectionType(collectionType);
        bean.setCourseName(courseName);

        ZYLearnService.setCollectionData(bean);

        return R.ok();
    }


    @PostMapping("cancelCourseCollection")
    @ApiOperation("取消课程收藏")
    public R cancelCourseCollection(@RequestBody Map map){

        String openId = (String) map.get("openId");
        String courseId = (String) map.get("courseId");
        String courseType = (String) map.get("courseType");
        String collectionType = (String) map.get("collectionType");
        if (openId==null || openId.length()<=0){
            return R.ok("openId 不存在");
        }
        if (courseId==null || courseId.length()<=0){
            return R.ok("courseId 不存在");
        }
        CollectionBean bean = new CollectionBean();
        bean.setOpenId(openId);
        bean.setCourseId(courseId);
        bean.setCourseType(courseType);
        bean.setCollectionType(collectionType);

        ZYLearnService.delCollectionData(bean);

        return R.ok();
    }

    @PostMapping("queryCollectionStatus")
    @ApiOperation("查询是否收藏")
    //    1收藏  2 为收藏
    public R queryCollectiionStatus(@RequestBody Map map){
        String openId = (String) map.get("openId");
        String courseId = (String) map.get("courseId");
        String courseType = (String) map.get("courseType");
        String collectionType = (String) map.get("collectionType");
        if (openId==null || openId.length()<=0){
            return R.ok("openId 不存在");
        }
        if (courseId==null || courseId.length()<=0){
            return R.ok("courseId 不存在");
        }
        CollectionBean bean = new CollectionBean();
        bean.setOpenId(openId);
        bean.setCourseId(courseId);
        bean.setCourseType(courseType);

        CollectionBean collectionBean= ZYLearnService.queryCollectionStatus(bean);
        CollectionStatusBean statusBean = new CollectionStatusBean();
        if (collectionBean==null){
            statusBean.setStatus("2");
        }else {
            statusBean.setStatus("1");
        }


        return R.ok().put("data",statusBean);
    }




}
